package com.thoughtservice.jspdoc.processors;


import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.thoughtservice.jspdoc.JSPFileList;
import com.thoughtservice.jspdoc.JSPPage;
import com.thoughtservice.jspdoc.RE;
import com.thoughtservice.jspdoc.RESyntaxException;
import com.thoughtservice.jspdoc.utils.ProcessorUtils;

public class ProcessTileInsertsIn implements DocProcessor{

	private final static Logger LOG = LoggerFactory.getLogger(ProcessTileInsertsIn.class);

	public boolean process(JSPPage p, String pc, JSPFileList fileList)
			throws RESyntaxException {
		LOG.trace("Entering {}", "ProcessTileInsertsIn");
		// For <tile:insert definition="" />
		RE re;		
		re = new RE("<tiles:insert[:space:]*definition=\"([:print:]*?)\"[:print:]*>");
		String[] matchRes = re.getAllMatches(pc, 1);
		LOG.debug("matchRes --> {}", new Object[]{matchRes});
					
		for(String s : matchRes){
			//String link = re.getParen(index);
			String link = s;
			
			try{
				//There are scenarios we need to die gracefully
				link = ProcessorUtils.figureLink(link, p, JSPPage.TILE_INSERTS_INPAGE);
			}catch (NullPointerException e) {
				LOG.error("Exception Occured and I am recovering from a NPE. {}", ExceptionUtils.getFullStackTrace(e));
			}			
		}
		
		//For <tile:insert attribute="" />
		re = new RE("<tiles:insert[:space:]*attribute=\"([:print:]*?)\"[:print:]*>");
		String[] attributeRes = re.getAllMatches(pc, 1);
		LOG.debug("matchRes --> {}", new Object[]{attributeRes});
					
		for(String s : attributeRes){
			//String link = re.getParen(index);
			String link = s;
			LOG.debug("link attr --> {}  p -->", link, p.getName());
			
			link = ProcessorUtils.figureLink(link, p, JSPPage.TILE_INSERTS_INPAGE);					
		}
		LOG.trace("Exiting {}", "ProcessTileInsertsIn");
	
		return false;
	}

}
